package com.ABdolphin.forum.controller;

import com.ABdolphin.forum.common.AppResult;
import com.ABdolphin.forum.common.ResultCode;
import com.ABdolphin.forum.config.AppConfig;
import com.ABdolphin.forum.exception.ApplicationException;
import com.ABdolphin.forum.model.Article;
import com.ABdolphin.forum.model.ArticleReply;
import com.ABdolphin.forum.model.User;
import com.ABdolphin.forum.services.IArticleReplyService;
import com.ABdolphin.forum.services.IArticleService;
import com.ABdolphin.forum.services.impl.ArticleReplyServiceImpl;
import com.ABdolphin.forum.services.impl.ArticleServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/reply")
@RestController
@Slf4j
@Tag(name = "Reply")
public class ReplyController {
    @Resource(type = ArticleReplyServiceImpl.class)
    private IArticleReplyService articleReplyService;

    @Resource(type = ArticleServiceImpl.class)
    private IArticleService articleService;

    @PostMapping("/create")
    @Operation(summary = "创建一个评论")
    public AppResult create(HttpServletRequest request,
                            @Parameter(name = "articleId", description = "文章id") @RequestParam("articleId") @NotNull Long articleId,
                            @Parameter(name = "content", description = "评论内容") @RequestParam("content") @NotNull String content,
                            @Parameter(name = "replyId", description = "父评论ID") @RequestParam(value = "replyId", required = false) Long replyId,
                            @Parameter(name = "replyUserId", description = "父评论作者ID") @RequestParam(value = "replyUserId", required = false) Long replyUserId) {
        User user = (User) request.getSession(false).getAttribute(AppConfig.USER_SESSION);
        //用户是否禁言
        if (user.getState() == 1) {
            log.info(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        if (replyId != null && replyUserId != null) {
            articleReply.setReplyId(replyId);
            articleReply.setReplyUserId(replyUserId);
        }
        articleReplyService.create(articleReply);
        return AppResult.success();
    }

    @GetMapping("/getReplies")
    @Operation(summary = "获得评论列表")
    public AppResult<List<ArticleReply>> getReplies(@Parameter(name = "articleId", description = "文章id") @RequestParam("articleId") @NotNull Long articleId) {
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        if (articleReplies == null) {
            articleReplies = new ArrayList<>();
        }
        return AppResult.success(articleReplies);
    }
    @PostMapping("/thumbUp")
    @Operation(summary = "点赞")
    public AppResult thumbsUp (HttpServletRequest request,
                               @Parameter(name = "id",description = "回复id")@RequestParam("id")@NotNull Long id){
        User user = (User) request.getSession(false).getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
            log.info(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }
        articleReplyService.addOneReplyLikeCountById(id);
        return AppResult.success();
    }


}
